#!/bin/bash

# showx  a small script select x manager and reconfig it.
# make sure you have files in currect direcotry.
# created by root@RainStudio

kdeCmd=/usr/bin/startkde
gnomeCmd=/usr/bin/gnome-session
enmentCmd=/usr/bin/enlightenment
afterstepCmd=/usr/bin/X11/afterstep
wmakerCmd=/usr/bin/wmaker
fvwm2Cmd=/usr/bin/X11/fvwm2
xfceCmd=/usr/bin/startxfce
iceCmd=/usr/bin/icewm

kcfgPath=$HOME/.kde/share/config
kcfgFile=kdeglobals
kcfgSav=kdeglobals.sav
kdevCfg=kdeglobals.dev

xcfgPath=/etc/X11
xcfg640=XF86Cfg640-4
xcfg800=XF86Cfg800-4
xcfg1024=XF86Cfg1024-4
xcfgFile=""

useKdev=0
dspMode=0

filePath=$HOME/.$(basename $0)
xType=0
chrAns=""
export XF86CONFIG

if ! [ -d $filePath ]; then
  mkdir $filePath
fi

if [ -e $HOME/.xinitrc ]; then
  cp -f $HOME/.xinitrc $filePath/.xinitrc
fi

if [ $# -gt 0 ]; then

  if [ $1 == "kde" ] && [ -e $kdeCmd ]; then
    echo $kdeCmd > $HOME/.xinitrc
    xType=1
  fi

  if [ $1 == "gnome" ] && [ -e $gnomeCmd ]; then
    echo $gnomeCmd > $HOME/.xinitrc
    xType=1
  fi

  if [ $1 == "enlightenment" ] && [ -e $enmentCmd ]; then
    echo $enmentCmd > $HOME/.xinitrc
    xType=1
  fi

  if [ $1 == "afterstep" ] && [ -e $afterstepCmd ]; then
    echo $afterstepCmd > $HOME/.xinitrc
    xType=1
  fi

  if [ $1 == "wmaker" ] && [ -e $wmakerCmd ]; then
    echo $wmakerCmd > $HOME/.xinitrc
    xType=1
  fi

  if [ $1 == "fvwm2" ] && [ -e $fvwm2Cmd ]; then
    echo $fvwm2Cmd > $HOME/.xinitrc
    xType=1
  fi
  
  if [ $1 == "ice" ] && [ -e $iceCmd ]; then
    echo $iceCmd > $HOME/.xinitrc
    xType=1
  fi
  
  if [ $1 == "xfce" ] && [ -e $xfceCmd ]; then
    xType=2
  fi

  for i in $*; do
   if [ $i == "kdev" ]; then
     if [ $xType -eq 0 ]; then
       xType=1
     fi
     useKdev=1
   fi
   if [ $i == "640" ]; then
     if [ $xType -eq 0 ]; then
       xType=1
     fi
     dspMode=1
   fi
   if [ $i == "800" ]; then
     if [ $xType -eq 0 ]; then
       xType=1
     fi
     dspMode=2
   fi
   if [ $i == "1024" ]; then
     if [ $xType -eq 0 ]; then
       xType=1
     fi
     dspMode=3
   fi
  done

  if [ $useKdev -eq 1 ]; then
    if [ -e $kcfgPath/$kcfgFile ]; then
      cp -f $kcfgPath/$kcfgFile $filePath/$kcfgSav
      if [ -e $filePath/$kdevCfg ]; then
        cp -f $filePath/$kdevCfg $kcfgPath/$kcfgFile
      else
        echo
        echo "error: can't find '$kdevCfg' in $filePath"
        echo 
        echo "hint:"
        echo "  1. how to create $kdevCfg ?"
        echo "   . modify a copy of file '$kcfgFile' and rename '$kdevCfg'"
        echo "   . place this file in $filePath"
        echo
        echo -n "continue load x?"
        read chrAns
        if [ -n $chrAns ] && [ $chrAns != "y" ] && [ $chrAns != "Y" ]; then
          xType=-1
        fi
        chrAns=""
      fi
    fi
  fi
    
  if [ $dspMode -gt 0 ]; then
    case $dspMode in
     1)
       xcfgFile=$xcfg640
       ;;
     2)
       xcfgFile=$xcfg800
       ;;
     3)
       xcfgFile=$xcfg1024
       ;;
    esac
    if [ -e $xcfgPath/$xcfgFile ]; then
      XF86CONFIG=$xcfgFile
    else
      echo
      echo "error: can't find '$xcfgFile' in $xcfgPath"
      echo
      echo "hint:"
      echo "  2. How to create $xcfgFile?"
      echo "   . login as root"
      echo "   . modify a copy of your x config file and rename '$xcfgFile'"
      echo "   . place this file in $xcfgPath"
      echo
      echo -n "continue load x?"
      read chrAns
      if [ -n $chrAns ] && [ $chrAns != "y" ] && [ $chrAns != "Y" ]; then
        xType=-1
      fi
      chrAns=""
    fi
  fi

  if [ $1 == "game" ] && [ -e /usr/bin/rsxinit ]; then
    xType=3
  fi

  case $xType in
    0)
      echo
      echo -n "message: can't find the windows manager , continue load x ?"
      read chrAns
      if [ -z $chrAns ] && [ $chrAns == "y" ] || [ $chrAns == "Y" ]; then
        chrAns=""
        startx
      else
        echo
      fi
      ;;
    1)
      startx
      ;;
    2)
      $xfceCmd
      ;;
    3)
      rsxinit 640
      ;;
    *)
      ;;
  esac

  if [ $useKdev -eq 1 ] && [ -e $filePath/$kcfgSav ] &&
    [ -e $kcfgPath/$kcfgFile ]; then
    cp -f $filePath/$kcfgSav $kcfgPath/$kcfgFile
    rm -f $filePath/$kcfgSav
  fi

else
  startx
fi

if [ -e $HOME/.xinitrc ]; then
  rm -f $HOME/.xinitrc
fi
if [ -e $filePath/.xinitrc ]; then
  cp -f $filePath/.xinitrc $HOME/.xinitrc
  rm -f $filePath/.xinitrc
fi
